package com.weiquan.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.weiquan.entity.AiDiagnosis;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * AI诊断Mapper接口
 */
@Mapper
public interface AiDiagnosisMapper extends BaseMapper<AiDiagnosis> {

    /**
     * 根据用户ID查询诊断历史
     */
    @Select("SELECT * FROM ai_diagnosis WHERE user_id = #{userId} AND is_deleted = 0 ORDER BY create_time DESC")
    List<AiDiagnosis> selectByUserId(@Param("userId") Long userId);

    /**
     * 统计用户诊断次数
     */
    @Select("SELECT COUNT(*) FROM ai_diagnosis WHERE user_id = #{userId} AND is_deleted = 0")
    Integer countByUserId(@Param("userId") Long userId);

    /**
     * 统计各场景的诊断数量
     */
    @Select("SELECT scenario, COUNT(*) as count FROM ai_diagnosis WHERE is_deleted = 0 GROUP BY scenario")
    List<Map<String, Object>> countByScenario();

    /**
     * 查询成功率统计
     */
    @Select("SELECT AVG(success_rate) as avg_rate, scenario FROM ai_diagnosis WHERE success_rate IS NOT NULL AND is_deleted = 0 GROUP BY scenario")
    List<Map<String, Object>> getSuccessRateStats();

    /**
     * 查询最近的诊断记录
     */
    @Select("SELECT * FROM ai_diagnosis WHERE user_id = #{userId} AND is_deleted = 0 ORDER BY create_time DESC LIMIT #{limit}")
    List<AiDiagnosis> selectRecentByUserId(@Param("userId") Long userId, @Param("limit") Integer limit);

    /**
     * 根据场景查询相似案例
     */
    @Select("SELECT * FROM ai_diagnosis WHERE scenario = #{scenario} AND success_rate >= #{minSuccessRate} AND is_deleted = 0 ORDER BY success_rate DESC LIMIT #{limit}")
    List<AiDiagnosis> selectSimilarCases(@Param("scenario") Integer scenario, @Param("minSuccessRate") Integer minSuccessRate, @Param("limit") Integer limit);
}